#include "selfcheckfail.h"
#include "ui_selfcheckfail.h"
#include "userlogger.h"
#include "log4z.h"
#include "systempreset.h"

using namespace zsummer::log4z;

SelfCheckFail::SelfCheckFail(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SelfCheckFail)
{
    bdf = BllDataFlow::getInstance();
    bCtrl = BllController::getInstance();

    ui->setupUi(this);

    connect(&m_timer, &QTimer::timeout, this, &SelfCheckFail::onTimeout);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &SelfCheckFail::retranslateUiSlot);

    if (windowWidth >= 1280) {
        this->setGeometry(0, 0, 1280, 800);
        this->setProperty("cssSize", "large");
        ui->label_2->setIndent(40);
    } else {
        this->setGeometry(0, 0, 1024, 600);
    }
    this->setWindowFlags(this->windowFlags() | Qt::SubWindow);
    setAttribute(Qt::WA_TranslucentBackground, true);
}

SelfCheckFail::~SelfCheckFail()
{
    delete ui;
}

void SelfCheckFail::show(const QString &error)
{
    UserLogger::error(error);
    ui->lblErrorText->setText(error);
    QDialog::show();
}

void SelfCheckFail::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void SelfCheckFail::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void SelfCheckFail::onTimeout()
{// 计时时间到，跳转到登录界面
    m_timer.stop();
    m_shutdown = false;
    bdf->changeMainPageIndex(MainPageIndex::LOGIN);
}

void SelfCheckFail::on_btnOK_pressed()
{
    m_shutdown = true;
    if (SystemPreset::instance()->getBoolData(EnumPresetItem::AllowLoginWhenBootFail))
    {// 长按，启动计时
        m_timer.start(3000);
    }
}

void SelfCheckFail::on_btnOK_released()
{
    m_timer.stop();
    this->hide();
    if (m_shutdown)
    {// 点击，关机
        ComCmd cmd = {CMD_TYPE::PWROFF, 0, 0, 0, 0, 0};
        bCtrl->sendPortCmd(cmd);
    }
}

